package com.oceanstar.controller;

import com.oceanstar.Pojo.*;
import com.oceanstar.service.IUserInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    private IUserInfoService userInfoService;

    /*
    注册
    */
    @PostMapping("/register")
    public Result enroll(@RequestBody UserInfo userInfo) throws Exception {
        userInfoService.insert(userInfo);
        return Result.success();
    }

    /*
    登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserInfo userInfo) {
        LoginInfo loginInfo = userInfoService.login(userInfo);
        if(loginInfo!=null){
            return Result.success(loginInfo);
        }
        else{
            return Result.error("用户名或密码错误");
        }
    }

    /*
    获取用户列表
     */
    @GetMapping("/users")
    public Result getUserList(UserQueryDTO queryDTO, HttpServletRequest request) {
        try {
            PageInfo<UserInfo> pageInfo = userInfoService.getUserList(queryDTO, request);
            return Result.success(pageInfo);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
    删除用户
     */
    @DeleteMapping("/users")
    public Result deleteUsers(@RequestParam("ids") Number[] ids) {
        try {
            userInfoService.deleteUsers(ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /*
    添加用户
     */
    @PostMapping("/users")
    public Result addUser(@RequestBody UserInfo userInfo) {
        try {
            userInfoService.insert(userInfo);
            return Result.success();
        } catch (Exception e) {
            return Result.error("添加失败: " + e.getMessage());
        }
    }

    /*
    更新用户信息
     */
    @PutMapping("/users")
    public Result updateUserInfoByAdministrator(@RequestBody UserInfo userInfo,@RequestHeader("token")String token) {
        try {
            userInfoService.updateUsersByAdmin(userInfo, token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    /*
    更新用户信息个人
     */
    @PatchMapping("/user/me")
    public Result updateUserInfoByUser(@RequestBody UserInfo userInfo,@RequestHeader("token")String token) {
        try {
            userInfoService.updateUsers(userInfo,token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    /*
    获取所有用户
     */
    @GetMapping("/users/list")
    public Result getAllUsers(@RequestHeader("token")String token) {
        try {
            List<UserInfo> userInfoList = userInfoService.getAllUsers(token);
            return Result.success(userInfoList);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
    获取用户个人信息
     */
    @GetMapping("/user/getinfo")
    public Result getUserInfo(@RequestHeader("token")String token ){
        UserInfo userinfo;
        userinfo = userInfoService.getUserInfo(token);
        return Result.success(userinfo);
    }

    /*
     *该接口用于绑定老人和家属
     */
    @PostMapping("family/bind")
    public Result bindFamily(@RequestBody BindFamily bindFamily, @RequestHeader("token") String token) {
        try {
            userInfoService.bindFamily(bindFamily, token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("绑定失败: " + e.getMessage());
        }
    }

    /*
     *该接口用于查询该家属绑定的老人
     */
    @GetMapping("family/bind")
    public Result bindFamily(@RequestHeader("token") String token) {
        try {
            SelectElderDTO elderDTO = userInfoService.selectElder(token);
            return Result.success(elderDTO);
        } catch (Exception e) {
            return Result.error("获取绑定失败: " + e.getMessage());
        }
    }

    /*
     *该接口用于查询老人绑定的家属信息
     */
    @GetMapping("family/binding")
    public Result getFamilyBinding(@RequestParam("id") Integer id) {
        try {
            List<SelectFamilyDTO> familyDTOList = userInfoService.selectFamily(id);
            return Result.success(familyDTOList);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}